from sqlalchemy import func
import hashlib

from system_manage import db


def set_password(pa):
    """
    密码加密
    """
    md5 = hashlib.md5('123')
    md5.update(pa.encode())
    return md5.hexdigest()


class BaseDatabase(db.Model):
    """
    定义父类模型
    """
    __abstract__ = True  # 只可以被继承,不可被执行#
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)  # 定义公有字段#

    def add(self):
        session = db.session
        session.add(self)
        session.commit()

    @staticmethod
    def update():
        session = db.session()
        session.commit()

    def delete(self):
        session = db.session()
        session.delete(self)
        session.commit()


# 职位与权限多对多关系表
Position_permission = db.Table(
    'position_permission',
    db.Column('pos_id', db.Integer, db.ForeignKey('position.id')),
    db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'))
)

# 员工与权限多对多关系表
person_permission = db.Table(
    'person_permission',
    db.Column('per_id', db.Integer, db.ForeignKey('person.id')),
    db.Column('permission_id', db.Integer, db.ForeignKey('permission.id'))
)


# 新闻表
class News(BaseDatabase):
    __tablename__ = 'news'
    title = db.Column(db.String(128))
    author = db.Column(db.String(32))
    content = db.Column(db.Text)
    picture = db.Column(db.String(128))
    publish_time = db.Column(db.Date)


# 部门表
class Department(BaseDatabase):
    __tablename__ = 'department'
    D_name = db.Column(db.String(32))
    D_des = db.Column(db.Text)
    # 员工部门反向映射
    D_P = db.relationship('PersonInfo',
                          backref='P_D')
    # 职位部门反向映射
    D_Pos = db.relationship('Position',
                            backref='Pos_D')


# 职位表
class Position(BaseDatabase):
    __tablename__ = 'position'
    Pos_name = db.Column(db.String(32))
    Pos_des = db.Column(db.Text)
    level = db.Column(db.Integer)
    # 员工与职位反向映射
    P_Per = db.relationship('PersonInfo',
                            backref='per_P')
    # 职位与权限的反向映射
    pos_permission = db.relationship('Permission',viewonly=False,
                                     secondary='position_permission',
                                     backref='permission_pos')
    # 部门职位外键
    pos_D = db.Column(db.Integer, db.ForeignKey('department.id'))


# 员工表
class Person(BaseDatabase):
    __tablename__ = 'person'
    P_name = db.Column(db.String(32))
    P_password = db.Column(db.String(32))
    # 员工考勤反向映射
    P_A = db.relationship('Attendance',
                          backref='A_P')
    # 员工kpi反向映射
    P_K = db.relationship('Kpi',
                          backref='K_P')
    # 员工信息员工反向映射
    Pe_P = db.relationship('PersonInfo',
                           backref='p_pe', uselist=False)
    # 员工与权限的反向映射
    per_permission = db.relationship('Permission',
                                     secondary=person_permission,
                                     backref='permission_per')


# 员工基本信息表
class PersonInfo(BaseDatabase):
    __tablename__ = 'personinfo'
    nickname = db.Column(db.String(32))
    sex = db.Column(db.String(32))
    age = db.Column(db.Integer)
    phone = db.Column(db.String(32))
    email = db.Column(db.String(32))
    work_number = db.Column(db.String(32))
    p_picture = db.Column(db.String(32))
    address = db.Column(db.Text)
    # 员工部门外键
    per_de = db.Column(db.Integer, db.ForeignKey('department.id'))
    # 员工职位外键
    per_pos = db.Column(db.Integer, db.ForeignKey('position.id'))
    # 员工信息员工外键
    per_per = db.Column(db.Integer, db.ForeignKey('person.id'))


# 员工考勤表
class Attendance(BaseDatabase):
    __tablename__ = 'attendance'
    status = db.Column(db.String(32), default='审核中')
    type = db.Column(db.String(32))
    cause = db.Column(db.Text)
    a_length = db.Column(db.Integer)
    start_time = db.Column(db.Date)
    end_time = db.Column(db.Date)
    Examiners = db.Column(db.String(32))
    # 员工考勤外键
    att_per = db.Column(db.Integer, db.ForeignKey('person.id'))


# 员工绩效表
class Kpi(BaseDatabase):
    __tablename__ = 'kpi'
    month = db.Column(db.Integer)
    kpi = db.Column(db.Float)
    des = db.Column(db.Text)
    # 员工kpi外键
    kpi_per = db.Column(db.Integer, db.ForeignKey('person.id'))


# 权限表
class Permission(BaseDatabase):
    __tablename__ = 'permission'
    per_name = db.Column(db.String(32))
    pe_des = db.Column(db.Text)


